import $cookie from 'spore-kit-cookie/cookie'
import { jsbridge, getCCTVJsApi } from './cctvJsApi'
import $logger from './logger'

const $log = $logger('seedCodeCookie').log

const seedCodeCookie = async () => {
	const ua = navigator.userAgent
	if (/yangshipin\/v([\d.]+)/i.test(ua)) {
		try {
			await getCCTVJsApi()
			await jsbridge.ready()
			const randomStr = await jsbridge.getCode('{"app_id":"100002"}')
			$cookie.set('vkey_ysp', randomStr.data, {
				expires: 1,
				domain: 'yangshipin.cn',
				path: '/'
			})
			$log($cookie.get('vkey_ysp'))
		} catch (error) {
			$log(error)
		}
	}
}

export default seedCodeCookie
